import { Color } from "cc";
import { EventId } from "../../../new/event/EventId";
import EventMgr from "../../../new/event/EventMgr";
import { BattleRole } from "../../../new/role/BattleRole";
import { DamageResult } from "../../../new/trigger/const/ITriggerResult";
import { BattleRoleUI } from "../../roleUI/BattleRoleUI";
import { UITriggerType } from "../UITriggerType";
import { ActionBase } from "./ActionBase";

/**
 * 行动-伤害计算行动(根据逻辑层伤害结果做表现)
 */
export class DamageAction extends ActionBase {

    targetUI: BattleRoleUI;
    result: DamageResult;
    constructor(casterUI: BattleRoleUI, targetUI: BattleRoleUI, result: DamageResult) {
        super();
        this.casterUI = casterUI;
        this.targetUI = targetUI;
        this.result = result;
    }

    excute(): void {
        console.log("触发伤害计算")
        let result: DamageResult = this.result;

        if (result.isHit) {
            //目标受击表现--格挡--被暴击
            if (result.isCritial) {
                // this.targetUI.UITriggerMgr.trigger(UITriggerType.beHit_ui)
                this.targetUI.onHit(1)
            } else {
                this.targetUI.onHit()
            }
            //受击特效

            //扣血
            this.targetUI.updateHp(-result.damage);
            console.log("扣血:" + result.damage)
            //飘字
            EventMgr.ins.event(EventId.FLOAT_TXT, { targetUI: this.targetUI, txt: `伤害:${result.damage}`, color: Color.RED })
            //???受击表现和死亡表现
            if (this.targetUI.isDie()) {
                this.targetUI.onDie();
            }
        } else {
            //做闪避表现
            this.targetUI.onMiss();
            console.log("闪避")
        }
    }

    check(params: any): boolean {
        if (params.damageIndex == this.result.damageIndex) {
            return true;
        }
        return false
    }
}